﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

public partial class SystemManage_ClientContacter : BasePage
{
    ISyClientHandler clientManage = new SyClientHandler();
    DataBase db = new DataBase();

    private static bool isUpdate = false;
    private static bool isDel = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!strPermission.Contains("1034"))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }
                else
                {
                    //判断用户有没有添加客户权限
                    if (strPermission.Contains("1035") || strPermission.Contains("1036"))
                    {
                        btnSave.Visible = true;
                    }
                    else
                    {
                        btnSave.Visible = false;
                    }
                    //判断用户有没有修改客户权限
                    if (strPermission.Contains("1036"))
                    {
                        isUpdate = true;
                    }
                    else
                    {
                        isUpdate = false;
                    }
                    //判断用户有没有删除客户权限
                    if (strPermission.Contains("1037"))
                    {
                        isDel = true;
                    }
                    else
                    {
                        isDel = false;
                    }

                }
            }

            ViewState["OrderBy"] = "PKID";
            ViewState["Accending"] = "asc";
            dataBind();
        }
    }

    /// <summary>
    /// 处理传入参数。。返回SQL条件和sqlparameter
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <param name="parms"></param>
    /// <summary>
    /// 处理传入参数。。返回SQL条件和sqlparameter
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <param name="parms"></param>
    private void ProcessParms(ref string sqlstr, ref SqlParameter[] parms)
    {
        List<SqlParameter> list = new List<SqlParameter>();
        sqlstr = "";

        if (Request["ClientID"] != null)
        {
            list.Add(new SqlParameter("@ClientID", Request["ClientID"]));
            sqlstr += " and ClientID=@ClientID";
        }

        if (ViewState["LinkMan"] != null)
        {
            list.Add(new SqlParameter("@LinkMan", ViewState["LinkMan"].ToString()));
            sqlstr += " and LinkMan like '%' +  @LinkMan + '%'";
        }      
        parms = list.ToArray();
    }


    private void dataBind()
    {
        string sqlStr = "SELECT * FROM Sy_ClientLinkMan where 1=1 ";
        SqlParameter[] parms = null;
        string clause = string.Empty;
        ProcessParms(ref clause, ref parms);
        sqlStr += clause;

        DataTable dt = clientManage.GetClientLinkManList(sqlStr, parms);
        DataView view = dt.DefaultView;
        view.Sort = ViewState["OrderBy"] + " " + ViewState["Accending"];
        gvClientList.DataSource = view;
        gvClientList.DataBind();
        if (dt != null && dt.Rows.Count > 0)
        {
            lblTotal.Text = dt.Rows.Count.ToString();
        }
        else
        {
            lblTotal.Text = "0";
        }
        //if (hidPKID.Text != string.Empty)
        //{
        //    lblPageType.Text = "修改客户联系人";
        //    btnSave.Text = "更新";
        //}

        //else
        //{
        //    lblPageType.Text = "添加客户联系人";
        //    btnSave.Text = "添加";
        //}

    }

    protected void gvClientList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvClientList.PageIndex = e.NewPageIndex;
        dataBind();
    }
    protected void gvClientList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            if (!clientManage.ClientLinkManDelete(int.Parse(e.CommandArgument.ToString())))
            {
                Jscript.AlertInAjax(Resources.Messages.DeleteFailed, UpdatePanel1);
            }
        }
        else if (e.CommandName == "Ud")
        {

            Sy_ClientLinkManQuery query = clientManage.GetClientLinkManByID(int.Parse(e.CommandArgument.ToString()));
            if (query != null)
            {
                txtLinkMan.Text = query.LinkMan;
                txtPhone.Text = query.TelNo;
                txtMobile.Text = query.MPhone;
                txtEmail.Text = query.Email;
                txtAddr.Text = query.Address;
                txtRemark.Text = query.Remark;
                txtFax.Text = query.Fax;
                txtPosition.Text = query.Position;
                //lblPageType.Text = "修改客户联系人";
                //btnSave.Text = "更新";
                hidPKID.Text = e.CommandArgument.ToString();
            }
        }
        //else if (e.CommandName == "Look")
        //{
        //    Sy_ClientLinkManQuery query = clientManage.GetClientLinkManByID(int.Parse(e.CommandArgument.ToString()));
        //    if (query != null)
        //    {
        //        txtLinkMan.Text = query.LinkMan;
        //        txtPhone.Text = query.TelNo;
        //        txtMobile.Text = query.MPhone;
        //        txtEmail.Text = query.Email;
        //        txtAddr.Text = query.Address;
        //        txtRemark.Text = query.Remark;
        //        txtFax.Text = query.Fax;
        //        txtPosition.Text = query.Position;
        //        lblPageType.Text = "客户联系人信息";
        //    }
            
        //}
        dataBind();

    }
    protected void gvClientList_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (e.SortExpression.ToLower() == ViewState["OrderBy"].ToString().ToLower())
        {
            if (ViewState["Accending"].ToString().ToLower() == "asc")
            {
                ViewState["Accending"] = "desc";
            }
            else
            {
                ViewState["Accending"] = "asc";
            }
        }
        else
        {
            ViewState["OrderBy"] = e.SortExpression;
        }

        dataBind();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {

        if (txtName.Text != string.Empty)
        {
            ViewState["LinkMan"] = txtName.Text.Trim();
        }
        else
        {
            ViewState["LinkMan"] = null;
        }
      
        dataBind();

    }
    protected void lbtnGo_Click(object sender, EventArgs e)
    {
        TextBox txtPages = ((TextBox)gvClientList.BottomPagerRow.FindControl("txtPages"));
        try
        {
            int i = Convert.ToInt32(txtPages.Text) - 1;
            if (i >= 0 && i < gvClientList.PageCount)
            {
                this.gvClientList.PageIndex = i;
                //数据绑定
                dataBind();
            }
            else
            {
                Jscript.AlertInAjax(Resources.Messages.WrongPageNumInput, UpdatePanel1);
                return;
            }
        }
        catch
        {
            Jscript.AlertInAjax(Resources.Messages.WrongPageNumInput, UpdatePanel1);
        }
    }
    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvClientList.PageSize = int.Parse(ddlPages.SelectedValue);
        dataBind();
    }
    protected void gvClientList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton btnDel = (LinkButton)e.Row.FindControl("lbtnDel");
           // Label lblEdit = (Label)e.Row.FindControl("lblEdit");
            LinkButton lblEdit = (LinkButton)e.Row.FindControl("lbtnEdit");
            if (isUpdate)
            {
                lblEdit.Visible = true;
            }
            else
            {
                lblEdit.Visible = false;
            }

            if (isDel)
            {
                btnDel.Visible = true;
            }
            else
            {
                btnDel.Visible = false;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
         Sy_ClientLinkManQuery query =new Sy_ClientLinkManQuery();
         Sy_ClientLinkManQuery newQuery = new Sy_ClientLinkManQuery();
        
        if (hidPKID.Text!= string.Empty)
        {
            query = clientManage.GetClientLinkManByID(int.Parse(hidPKID.Text));
            if (query != null)
            {
                query.LinkMan = txtLinkMan.Text.Trim();
                query.TelNo = txtPhone.Text.Trim();
                query.Position = txtPosition.Text.Trim();
                query.Email = txtEmail.Text.Trim();
                query.Fax = txtFax.Text.Trim();
                query.Remark = txtRemark.Text.Trim();
                query.Address = txtAddr.Text.Trim();
                query.MPhone = txtMobile.Text.Trim();

                if (clientManage.ClientLinkManUpdate(query))
                {
                    Jscript.AlertInAjax(Resources.Messages.UpdateSuccess, UpdatePanel1);
                }
                else
                {
                    Jscript.AlertInAjax(Resources.Messages.UpdateFailed, UpdatePanel1);

                }
            }
        }
        else
        {
            query.LinkMan = txtLinkMan.Text.Trim();
            query.TelNo = txtPhone.Text.Trim();
            query.Position = txtPosition.Text.Trim();
            query.Email = txtEmail.Text.Trim();
            query.Fax = txtFax.Text.Trim();
            query.Remark = txtRemark.Text.Trim();
            query.Address = txtAddr.Text.Trim();
            query.MPhone = txtMobile.Text.Trim();
            if (Request["ClientID"] != null)
            {
                query.ClientID = int.Parse(Request["ClientID"]);
            }
            else
            {
                query.ClientID = 0;
            }

            if (clientManage.ClientLinkManInsert(query))
            {
                Jscript.AlertInAjax(Resources.Messages.AddSuccess, UpdatePanel1);

            }
            else
            {
                Jscript.AlertInAjax(Resources.Messages.AddFailed, UpdatePanel1);

            }         

        }
        Clear();
        dataBind();
    }

    private void Clear()
    {
        txtLinkMan.Text = string.Empty;
        txtPhone.Text = string.Empty;
        txtMobile.Text = string.Empty;
        txtEmail.Text = string.Empty;
        txtAddr.Text = string.Empty;
        txtRemark.Text = string.Empty;
        txtFax.Text = string.Empty;
        txtPosition.Text = string.Empty;
        hidPKID.Text = string.Empty;

    }

    //private void IsEnable(bool istrue)
    //{
    //    txtLinkMan.Enabled = istrue;
    //    txtPhone.Enabled = istrue;
    //    txtMobile.Enabled = istrue;
    //    txtEmail.Enabled = istrue;
    //    txtAddr.Enabled = istrue;
    //    txtRemark.Enabled = istrue;
    //    txtFax.Enabled = istrue;
    //    txtPosition.Enabled = istrue;
    //}

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("ClientList.aspx");
      
    }
}
